<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/head.jsp"%>
<div class="right">
    <form action="${pageContext.request.contextPath}<c:if test="${opr == 'add'}">/user/add</c:if><c:if test="${opr == 'update'}">/user/update</c:if>" id="myForm" method="post" enctype="multipart/form-data">
        <table style="margin-left: 120px;margin-top:50px;line-height: 40px">
            <tr>
                <th>账号:</th>
                <td><input id="account" name="account" type="text" value="${user.account}"><span id="valAccount" style="color: red"></span></td>
            </tr>
            <tr>
                <th>姓名:</th>
                <td><input id="realName" name="realName" type="text" value="${user.realName}"></td>
            </tr>
            <tr>
                <th>密码:</th>
                <td><input id="password" name="password" type="password" value="${user.password}"></td>
            </tr>
            <tr>
                <th>确认密码:</th>
                <td><input id="password2" name="password2" type="password" value="${user.password}"></td>
            </tr>
            <tr>
                <th>性别:</th>
                <td>
                    <select name="sex">
                        <option value="1" <c:if test="${user.sex == 1}">selected</c:if>>男</option>
                        <option value="2" <c:if test="${user.sex == 2}">selected</c:if>>女</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th>出生日期:</th>
                <td><input id="birthday" name="birthday" type="date" value="<fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"/>"></td>
            </tr>
            <tr>
                <th>电话号码:</th>
                <td><input id="phone" name="phone" type="text" value="${user.phone}"></td>
            </tr>
            <tr>
                <th>地址：</th>
                <td><input id="address" name="address" type="text" value="${user.address}"></td>
            </tr>
            <tr>
                <th>角色：</th>
                <td>
                    <select name="roleId">
                        <c:forEach items="${roleList}" var="role">
                            <option value="${role.id}" ${user.roleId == role.id ? "selected" : ""}>${role.roleName}</option>
                        </c:forEach>
                    </select>
                </td>
            </tr>
            <tr>
                <th>证件照：</th>
                <td><input type="file" name="idPic"></td>
            </tr>
            <tr style="display: none">
                <th>id：</th>
                <td><input type="text" name="id" value="${user.id}"></td>
                <c:if test="${opr == 'add'}">
                    <th>创建人id：</th>
                    <td><input name="createdUserId" type="text" value="${userSession.id}"></td>
                    <th>创建时间：</th>
                    <td><input id="createdTime" name="createdTime" type="date"></td>
                </c:if>
                <c:if test="${opr == 'update'}">
                    <th>修改人id：</th>
                    <td><input name="updatedUserId" type="text" value="${userSession.id}"></td>
                    <th>修改时间：</th>
                    <td><input id="updatedTime" name="updatedTime" type="date"></td>
                </c:if>
            </tr>
        </table>
        <input type="submit" value="提交" style="width: 80px;height: 30px;margin-left: 160px;text-align: center;">
    </form>
</div>
</section>
<script src="${pageContext.request.contextPath}/statics/js/jquery-1.8.3.min.js"></script>
<script>
    $(document).ready(function (){
        var time = new Date()
        var day = ('0' + time.getDate()).slice(-2)
        var month = ('0' + (time.getMonth() + 1)).slice(-2)
        var today = time.getFullYear() + '-' + month + '-' + day;
        $("#createdTime").val(today);
        $("#updatedTime").val(today);
        $("#myForm").submit(function (){
            var account = $("#account").val();
            var realName = $("#realName").val();
            var password = $("#password").val();
            var password2 = $("#password2").val();
            var birthday = $("#birthday").val();
            var phone = $("#phone").val();
            var address = $("#address").val();
            if (account == null || account == ""){
                alert("账号不能为空！");
                return false;
            }
            if (realName == null || realName == ""){
                alert("姓名不能为空！");
                return false;
            }
            if (password == null || password == ""){
                alert("密码不能为空！");
                return false;
            }
            if (password != password2){
                alert("前后两次密码不一致！");
                return false;
            }
            if (birthday == null || birthday == ""){
                alert("生日不能为空！");
                return false;
            }
            if (phone == null || phone == ""){
                alert("电话号码不能为空！");
                return false;
            }
            if (address == null || address == ""){
                alert("地址不能为空！");
                return false;
            }
            return true;
            // return false;
        })
        $("#account").blur(function (){
            var account = $(this).val();
            var valAccount = $("#valAccount");
            if (account==""){
                valAccount.text("账号不能为空！");
                return ;
            }
            $.get("${pageContext.request.contextPath}/user/validateAccount","account="+account,function (data){
                console.log(data);
                if (data==="true"){
                    valAccount.text("账号可用！");
                }
                if (data==="false"){
                    valAccount.text("账号重复！");
                }
            })
        })
    })
</script>
<%@include file="/WEB-INF/jsp/common/foot.jsp" %>
